package com.hu.article.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.hu.common.pojo.Blog;
import com.hu.common.vo.BlogChartsVo;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * <p>
 *  Mapper 接口
 * </p>
 *
 * @author hu
 * @since 2023-09-07
 */
public interface BlogMapper extends BaseMapper<Blog> {

    @Select("select count(*) as num, time as publishDate from blog where time between #{startTime} and #{endTime} group by time")
    List<BlogChartsVo> findBlogCharts(String startTime, String endTime);

    @Transactional
    @Update("update blog set read_count = read_count + 1 where id = #{id}")
    void addReadCount(Integer id);

    @Update("update blog set comment_count = comment_count + 1 where id = #{blogId}")
    void addCommentCount(Integer blogId);

    @Update(("update blog set comment_count = comment_count - #{deleteCount} where id = #{blogId}"))
    void reduceCommentCount(@Param("blogId") Integer blogId, @Param("deleteCount") int deletedCount);

    @Update("update blog set praise = praise + 1 where id = #{blogId}")
    void addPraise(@Param("blogId") Integer blogId);
}
